package com.ruoyi.cfuser.rescue.listener;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.rabbitmq.client.Channel;
import com.ruoyi.cfuser.api.rescue.domain.RescueTask;
import com.ruoyi.cfuser.api.rescue.domain.ServiceType;
import com.ruoyi.cfuser.api.rescue.domain.ShopServiceType;
import com.ruoyi.cfuser.rescue.service.impl.RescueTaskServiceImpl;
import com.ruoyi.cfuser.service.impl.ServiceTypeServiceImpl;
import com.ruoyi.common.redis.service.RedisService;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

@Component
public class TaskListener {
    @Resource
    private RescueTaskServiceImpl rescueTaskService;
    @Autowired
    private RedisService redisService;
    @Resource
    private ServiceTypeServiceImpl serviceTypeService;

    @RabbitListener(queues = "task_dlx_queue") // 监听死信队列
    public void listenerMessage(Message message, Channel channel) throws IOException {
        String strId = new String(message.getBody());
        long taskId = Long.parseLong(strId);
        RescueTask rescueTask = rescueTaskService.selectRescueTaskById(taskId);
        ServiceType serviceType = new ServiceType();
        serviceType.setName("维修");
        if (rescueTask.getStatus() == 0) {
            //  获取包含维修的所有商家id
            List<ShopServiceType> shopServices = serviceTypeService.selectServiceTypeToShop(serviceType).getShopServices();
            for (ShopServiceType shopServiceType : shopServices) {
                System.out.println("----"+shopServiceType);
                List<String> cacheList = redisService.getCacheList("taskList" + shopServiceType.getMid());
                if (cacheList.size() > 0) {
                    // 删除redis中的数据
                    for (int i = 0; i < cacheList.size(); i++) {
                        RescueTask bean1 = JSONUtil.toBean(cacheList.get(i), RescueTask.class);
                        RescueTask bean2 = JSONUtil.toBean(JSON.toJSONString(rescueTask), RescueTask.class);
                        if (bean1.equals(bean2)) {  // 移除redis中的超时订单
                            cacheList.remove(i);
                            System.out.println("相等了");
                        }
                    }
                    redisService.deleteObject("taskList" + shopServiceType.getMid());
                    if (cacheList.size() > 0) {
                        redisService.setCacheList("taskList" + shopServiceType.getMid(), cacheList);
                    }else {
                        redisService.deleteObject("taskList" + shopServiceType.getMid());
                    }
                }
            }
            rescueTask.setStatus(2);
            rescueTaskService.updateRescueTask(rescueTask);
        //  手动签收
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
            System.out.println("订单超时");
        }else {
        //  手动签收
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
            System.out.println("商家已接单");
        }
    }
}
